package com.shenfa.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.City;
import com.shenfa.bean.CostPar;
import com.shenfa.bean.Epiboly;
import com.shenfa.bean.PaperCityCrd;
import com.shenfa.dao.CityDao;
import com.shenfa.dao.CostParDao;
import com.shenfa.dao.EpibolyDao;
import com.shenfa.util.sequence.SequenceUtil;

/**
 * 外包商信息业务逻辑类
 * 
 * @author yanglong
 * 
 * @date 2011-8-22
 *
 */
public class EpibolyService {

	@Autowired
	private EpibolyDao epibolyDao;
	
	@Autowired
	private SequenceUtil sequenceUtil;
	
	@Autowired
	private CityDao cityDao;
	
	@Autowired
	private CostParDao costParDao;

	/**
	 * 添加外包商
	 * @param t	外包商	
	 * @return	0成功，-1失败
	 */
	public int add(Epiboly t) {
		t.setId(sequenceUtil.getSequence(SequenceUtil.SEQ_EPLBOLY_PNT));
		int num= epibolyDao.add(t);
		
		if(num==0){
			List<City> city=cityDao.queryAllList();
			CostPar costPar=new CostPar();
//			costPar.setEpibolyname(t.getName());
			costPar.setEpibolyno(t.getId());
			costPar.setEnvcost(0);
			costPar.setEnvelop(0);
			costPar.setEnvlopweight(0);
			costPar.setPrint(0);
			costPar.setReckoning(0);
			costPar.setTotalweight(0);
			if(city.size()>0){
				for (City city2 : city) {
	//				costPar.setCityname(city2.getCityname());
					costPar.setCityno(city2.getCityno());
					costParDao.add(costPar);
				}
			}else{
				num = -1;
			}
		}
		return num;
	}

	/**
	 * 删除外包商
	 * @param t	外包商
	 * @return	0成功，-1失败
	 */
	public int del(Epiboly t) {
		int num=epibolyDao.del(t);
		if(num==0){
			CostPar costPar=new CostPar();
			costPar.setEpibolyno(t.getId());
			try{
				costParDao.del(costPar);
			}catch(Exception e){
				e.printStackTrace();
				num = -1;
			}
		}
		return num;
	}

	/**
	 * 更新外包商
	 * @param t	外包商
	 * @return	0成功，-1失败
	 */
	public int upd(Epiboly t) {
		return epibolyDao.upd(t);
	}
	
	/**	
	 * 根据对象条件查询外包商列表
	 * @param t	外包商查询条件
	 * @return	外包商列表
	 */
	public List<Epiboly> getList(Epiboly t,int start,int limit) {
		return epibolyDao.getList(t,start,limit);
	}
	public int queryCount(Epiboly t) {
		return epibolyDao.queryCount(t);
	}

	/**
	 * 查询卡产品关联的外包商列表
	 * @param p
	 * @return
	 */
	public List<Epiboly> getCondiList(PaperCityCrd p) {
		return epibolyDao.getCondiList(p);
	}
	
	/**
	 * 查询所有外包商列表
	 */
	public List<Epiboly> getAllList() {
		return epibolyDao.getAllList();
	}
}
